package com.totoro.rabbitmq.java;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * RabbitMQ纯java实现消息接收
 * Created by Niki on 2018/3/21 15:49
 */
public class RabbitMQReceive {
    private static final String QUEUE_NAME = "HELLO";

    public static void receive() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "utf-8");

            }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);

    }

}
